import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/Home.vue'
import tianBao from '../components/tianBaoLeft.vue'
import ShiList from '../components/cgtbsh/shiList.vue'
import WenList from '../components/cgtbsh/wenList.vue'
import TuList from '../components/cgtbsh/tuList.vue'
import YanList from '../components/cgtbsh/yanList.vue'
import ShowMa from '../components/cgsjzs/showCusMap.vue'
import MapMl from '../components/cgsjzs/mapLeft.vue'
import Aside from '../components/cgzh/ZhAsideBar'
import theAtlas from '../components/cgzhgl/TheAtlas'
import theVector from "../components/cgzhgl/TheVector";
import theText from "../components/cgzhgl/TheText";
import theResearchReport from  "../components/cgzhgl/TheResearchReport"
import theBackupList from "../components/cgzh/TheBackupList";
import theBackupRecovery from "../components/cgzh/TheBackupRecovery";
import theBackupLog from "../components/cgzh/TheBackupLog";
import zhglLeft from '../components/cgzhgl/zhglLeft'
import zhglMain from '../components/cgzhgl/zhglMain'
import houtaiLeft from '../components/htgl/houTaiLeft'
import YhList from '../components/htgl/yhList'
import jueseList from '../components/htgl/jueseList'
import buMenList from '../components/htgl/buMenList'
import quanxianList from '../components/htgl/quanxianList'
import riZhiList from "../components/htgl/riZhiList";
import jcLeft from '../components/hjjczc/jcLeft'
import showjcMap from '../components/hjjczc/showJueCeMap'
import gxmain from '../components/cgsjgx/gxmain'
import leftAsideBar from "../components/cgzhgl/leftAsideBar";
import CatalogManage from "../components/cgzh/CatalogManage";
import TbAsideBar from "@/components/cgtbsh/TbAsideBar";
import shouye from "../components/sy/shouye";
import theReview from "@/components/cgtbsh/TheReview"
import theApproved from '@/components/cgtbsh/Theapproved'
import instructions from "@/components/cgtbsh/Instructions"
Vue.use(VueRouter)

const routes = [
{
path: '/',
redirect: '/login'
},
{
path: '/login',
component: () => import('@/components/login'),
},
{
path: "/404",
component: () => import('@/components/common/404')
},
]

/*const routes = [
{
path: '/',
redirect: '/login'
},
{
path: '/login',
component: Login,
meta: {title:'三线一单系统-登录页面'}
},
{
path: '/home',
component: Home,
redirect: '/shiList',
children: [
{
path: '/shiList',
components: {
'main': ShiList,
'left': TbAsideBar
}
},
{
path: '/wenList',
components: {
'main': WenList,
'left': TbAsideBar
}
},
{
path: '/tuList',
components: {
'main': TuList,
'left': TbAsideBar
}
},
{
path: '/yanList',
components: {
'main': YanList,
'left': TbAsideBar
}
},
{
path: '/review',
components: {
'main': theReview,
'left': TbAsideBar
}
},
{
path: '/approved',
components: {
'main': theApproved,
'left': TbAsideBar
}
},
{
path: '/instructions',
components: {
'main': instructions,
'left': TbAsideBar
}
}
]
},
{
path: '/showData',
component: Home,
redirect: '/mapList',
children: [
{
path: '/mapList',
components: {
'left': MapMl,
'main': ShowMa,
},
meta: {title:'三线一单系统-成果数据展示'}
},
],

},
{
path: '/zhData',
component: Home,
redirect: '/catalog',
children: [
{
path: '/catalog',
components: {
'left': Aside,
'main': CatalogManage
}
},
]
},
{
path: '/zhonghe',
component: Home,
redirect: '/theVector',
children: [
{
path: '/theVector',
components: {
'left': leftAsideBar,
'main': theVector,
}
},
{
path: '/theAtlas',
components: {
'left': leftAsideBar,
'main': theAtlas
}
},
{
path: '/theText',
components: {
'left': leftAsideBar,
'main': theText
}
},
{
path: '/theReport',
components: {
'left': leftAsideBar,
'main': theResearchReport
}
},
{
path: '/theBackupList',
components: {
'left': leftAsideBar,
'main': theBackupList
}
},
{
path: '/theBackupRecovery',
components: {
'left': leftAsideBar,
'main': theBackupRecovery
}
},
{
path: '/theBackupLog',
components: {
'left': leftAsideBar,
'main': theBackupLog
}
},
{
path: '/yxList',
components: {
'main': zhglMain ,
'left': zhglLeft
}
},
{
path: '/zdList',
components: {
'main': WenList,
'left': tianBao
}
},
{
path: '/ybList',
components: {
'main': TuList,
'left': tianBao
}
},
]
},
{
path: '/houtai',
component: Home,
redirect: '/yhList',
children: [
{
path: '/yhList',
components: {
'main': YhList,
'left': houtaiLeft
},
meta: {title:'三线一单系统-后台用户管理'}
},
{
path: '/jsList',
components: {
'main': jueseList,
'left': houtaiLeft
},
meta: {title:'三线一单系统-后台角色管理'}
},
{
path: '/bmList',
components: {
'main': buMenList,
'left': houtaiLeft
},
meta: {title:'三线一单系统-后台部门管理'}
},
{
path: '/qxList',
components: {
'main': quanxianList,
'left': houtaiLeft
},
meta: {title:'三线一单系统-后台资源管理'}
},
{
path: '/rzList',
components: {
'main': riZhiList,
'left': houtaiLeft
},
meta: {title:'三线一单系统-后台日志管理'}
},
{
path: '/xtList',
components: {
'main': YanList,
'left': houtaiLeft
},
meta: {title:'三线一单系统-后台系统管理'}
},
]
},
{
path: '/hjjczc',
component: Home,
redirect: '/jcfx',
children: [
{
path: '/jcfx',
components: {
'left': jcLeft,
'main': showjcMap,
}
},
]
},
{
path: '/gongxiang',
component: Home,
redirect: '/share',
children: [
{
path: '/share',
components: {
'main': gxmain,
}
},
]
},
{
path: '/shouye',
component: Home,
redirect: '/shouye',
children: [
{
path: '/shouye',
components: {
'main': shouye,
}
},
]
},
]*/

const router = new VueRouter({
mode:'hash',
routes
// constantRoutes
})

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return routerPush.call(this, location).catch(error=> error)
}
var arr = []
router.beforeEach((to, from, next) => {
if (sessionStorage.token) {
if (sessionStorage.menu) {
if(arr.length==0){
var me=JSON.parse(sessionStorage.menu)
me.forEach((item,index) => {
if (item.children) {
let newArr = {
path: item.parent.path,
name: item.parent.name,
component:  require('@/components/'+item.parent.component).default,
meta: {title:item.parent.name},
redirect:item.parent.redirectPath,
children: []
};
if(index==0){
window.sessionStorage.setItem('activePath',item.parent.path)
}
arr.push(newArr);
item.children.forEach(child => {
if (me[index].children) {
me[index].children.find(userItem => {
let newArr2 = {
path: userItem.parent.path,
name: userItem.parent.name,
components:userItem.parent.component.split('||').length==2?{'main':require('@/components'+userItem.parent.filePath.split('||')[0]).default,'left':require('@/components'+userItem.parent.filePath.split('||')[1]).default}:{'main':require('@/components'+userItem.parent.filePath.split('||')[0]).default},
meta: {title:userItem.parent.name},
};
userItem.parent.path == child.parent.path ? arr[arr.length ? arr.length - 1 : arr.length].children.push(newArr2) : arr = arr;
})
}
})
}
})
arr.push(  {
path: "*",
redirect: "/404"
})
console.log(arr)
router.options.routes = router.options.routes.concat(arr)
router.addRoutes(arr)

next({
...to,
replace: true
})
}else {
console.log(1)
next();
}
}else {
next({
...to,
replace: true
})
}
}else {
if(to.path !== '/login'){
next({ path: '/login' })
next()
}else {
next()
}
}
/* if (to.path === '/login') return next()
const tokenStr = window.sessionStorage.getItem('token')
if (!tokenStr) return  next({ path: '/login' })
next()
*/
if (to.meta.title) {
document.title = to.meta.title
}
next()
})
export default router
